<?php

/**
 * Class CollectModule
 * 数据表 id user_id gopen_id create_time
 */
class CollectModule extends SiteMode {
    /** @return CartMode */
    public static function init() {
        return parent::init();
    }

    // select * ；收藏夹没有detail可言
    public function getList($user_id,$rows=30,$offset=0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql="SELECT SQL_CALC_FOUND_ROWS * FROM `user_collect` WHERE `user_id`=? and `site_id`=? order by `create_time` DESC LIMIT $offset,$rows";
        return $this->db->getList($sql, $user_id, $this->siteId);
    }

    public function add($user_id,$item_id){
        $data['user_id']=(int)$user_id;
        $data['gopen_id']=(int)$item_id;
        $data['site_id']=$this->siteId;
        if($this->db->insert('user_collect',$data) == 1){
            return (int)$this->db->insertId();
        }
        return -1;
    }
    // 移出
    public function remove($id){
        $id=(int)$id;
        return $this->db->delete('user_collect',$id)==1;
    }

    // 检查是否收藏了
    function checkExist($user_id,$item_id){
        $sql="SELECT 1 FROM `user_collect` WHERE `user_id`=? and `gopen_id`=? and `site_id`=?";
        return $this->db->getExist($sql,$user_id,$item_id, $this->siteId);
    }

    // item被收藏次数
    public function getNum($item_id){
        $sql="SELECT count(*) FROM `user_collect` WHERE `gopen_id`=? and `site_id`=?";
        return $this->db->getValue($sql,$item_id, $this->siteId);
    }
} 